import 'package:equatable/equatable.dart';
import 'package:backend.core/core/domain/entities/api/app_store/api_app_store_one_time_purchased_item.dart';
import 'package:backend.purchases/infrastructure/dtos/api/app_store/api_app_store_one_time_purchased_item_transaction_dto.dart';
import 'package:backend.purchases/infrastructure/dtos/api/app_store/api_app_store_purchased_item_dto.dart';

const kApiAppStoreOneTimePurchasedItemTransactionIdKey = 'transaction_id';
const kApiAppStoreOneTimePurchasedItemRevokedKey = 'revoked';
const kApiAppStoreOneTimePurchasedItemTransactionKey = 'transaction';

const kApiAppStoreOneTimePurchasedItemTypeValue = 'one_time_purchased_item';

class ApiAppStoreOneTimePurchasedItemDto extends Equatable implements ApiAppStorePurchasedItemDto {
  const ApiAppStoreOneTimePurchasedItemDto._internal({
    required this.purchaseItemSku,
    required this.test,
    required this.transactionId,
    required this.revoked,
    required this.transactionDto,
  });

  @override
  final String purchaseItemSku;

  @override
  final bool test;

  final String transactionId;
  final bool revoked;
  final ApiAppStoreOneTimePurchasedItemTransactionDto transactionDto;

  @override
  List<Object?> get props {
    return [
      purchaseItemSku,
      test,
      transactionId,
      revoked,
      transactionDto,
    ];
  }

  factory ApiAppStoreOneTimePurchasedItemDto.fromEntity(
      ApiAppStoreOneTimePurchasedItem oneTimePurchasedItem) {
    final transactionDto =
        ApiAppStoreOneTimePurchasedItemTransactionDto.fromEntity(oneTimePurchasedItem.transaction);

    return ApiAppStoreOneTimePurchasedItemDto._internal(
      purchaseItemSku: oneTimePurchasedItem.purchaseItemSku,
      test: oneTimePurchasedItem.test,
      transactionId: oneTimePurchasedItem.transactionId,
      revoked: oneTimePurchasedItem.revoked,
      transactionDto: transactionDto,
    );
  }

  @override
  Map<String, dynamic> toJson() {
    return {
      kApiAppStorePurchasedItemTypeKey: kApiAppStoreOneTimePurchasedItemTypeValue,
      kApiAppStorePurchasedItemPurchaseItemSkuKey: purchaseItemSku,
      kApiAppStorePurchasedItemTestKey: test,
      kApiAppStoreOneTimePurchasedItemTransactionIdKey: transactionId,
      kApiAppStoreOneTimePurchasedItemRevokedKey: revoked,
      kApiAppStoreOneTimePurchasedItemTransactionKey: transactionDto.toJson(),
    };
  }
}
